package com.yanqu.road.server.cmd.activity.bulletscreen;

import com.yanqu.road.entity.servercenter.GameEditLimit;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.activity.BulletScreenProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.logic.thread.BulletScreenTask;
import com.yanqu.road.server.manger.ForbiddenMgr;
import com.yanqu.road.server.manger.GameEditLimitMgr;
import com.yanqu.road.server.manger.activity.newyear2023.FireWorkMgr;
import com.yanqu.road.server.manger.activity.springdinner.SpringDinnerMgr;
import com.yanqu.road.server.manger.player.WorldMessageMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = GameProtocol.S_ACTIVITY_BULLET_SCREEN_COMMENTS, desc = "弹幕")
public class BulletScreenContentCmd extends AbstractCommand {

    String content;
    int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BulletScreenProto.BulletScreenContentReqMsg reqMsg = BulletScreenProto.BulletScreenContentReqMsg.parseFrom(packet.getBytes());
        content = reqMsg.getContent();
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        //编辑限制
        GameEditLimit gameEditLimit = GameEditLimitMgr.getGameEditLimit(Protocol.S_ACTIVITY_BULLET_SCREEN_COMMENTS);
        if(null != gameEditLimit){
            long nowTime = System.currentTimeMillis();
            if(nowTime >= gameEditLimit.getLimitEditStartTime() && nowTime < gameEditLimit.getLimitEditEndTime()){
                String replaceStr = "祝掌柜们财源广进！";
                if(!StringUtils.isNullOrEmpty(gameEditLimit.getReplaceContent())){
                    replaceStr = gameEditLimit.getReplaceContent();
                }
                content = replaceStr;
            }
        }
        /**
         * 如果被禁用 则返回系统未开放
         */
        if (ForbiddenMgr.isForbiddenCmd(player.getUserId(), Protocol.S_ACTIVITY_BULLET_SCREEN_COMMENTS)) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        int len = StringUtils.getStringLen(content);
        //年夜饭的校验不一样，因为年夜饭还有个固定长度加昵称，这里直接限制一百长度
        if(activityId == SpringDinnerMgr.getShowTimeActivityId()){
            if(len > 100){
                ret = GameErrorCode.E_ACTIVITY_BARRAGE_CONTENT_TOO_LONG;
                return;
            }
        }else if(activityId == FireWorkMgr.getRedPacketActivityId()){
            //烟花祈愿
            if(len > 100){
                ret = GameErrorCode.E_ACTIVITY_BARRAGE_CONTENT_TOO_LONG;
                return;
            }
        }else{
            if(len > GameConfig.ACTIVITY_BULLET_SCREEN_CONTENT_LEN){
                ret = GameErrorCode.E_ACTIVITY_BARRAGE_CONTENT_TOO_LONG;
                return;
            }
        }
        GeneralTask task = new BulletScreenTask(player, content, activityId);
        WorldMessageMgr.addTask(task);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0) {
            BulletScreenProto.BulletScreenContentRespMsg.Builder respMsg = BulletScreenProto.BulletScreenContentRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setActivityId(activityId);
            player.sendPacket(ClientProtocol.U_ACTIVITY_BULLET_SCREEN_COMMENTS, respMsg);
        }
    }
}
